package helperClassSites;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import utils.Utils;

public class CreateProfiles {
	public static void CreateProfilesFiles(String path, int xlsSheet){
		try {
			Workbook workbook = Workbook.getWorkbook(new File("C:\\Documents and Settings\\Administrator\\My Documents\\Dropbox\\CTA\\XLS\\CTA.xls"));
			Sheet sheet = workbook.getSheet(xlsSheet);
			Cell[] count = sheet.getColumn(0);

			for (int i = 1; i < count.length; i++) {
				Cell siteNameCell = sheet.getCell(9, i);
				String sitename = siteNameCell.getContents();

				File replacedFile = new File(path + sitename + ".html");
				replacedFile.delete();
				FileWriter fstream = new FileWriter(path + sitename + ".html");
				BufferedWriter textFile = new BufferedWriter(fstream);

				textFile.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n");
				textFile.write("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> \n");
				textFile.write(" <!-- \n");
				textFile.write(" Copyright 2010 Google Inc.\n");
				textFile.write(" Licensed under the Apache License, Version 2.0:\n");
				textFile.write(" http://www.apache.org/licenses/LICENSE-2.0 \n");
				textFile.write(" --> \n");
				textFile.write("<html> \n");
				textFile.write("<head> \n");
				textFile.write("<meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" />\n");
				Cell passNameCell = sheet.getCell(4, i);
				String passNameEN = passNameCell.getContents();
				String replaced = Utils.replaceUmlautsDeutsch(passNameEN);
				textFile.write("<meta name=\"Description\" content=\"Profile of the " + replaced + ". This profile shows the hight difference of the pass and let you explore the " + replaced
						+ " hight differences. There are around 480 passes on www.cyclingthealps.com. Come and have a look.\" />\n");
				textFile.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/> \n");
				textFile.write("<style type=\"text/css\">\n");
				textFile.write("  html { height: 100% }\n");
				textFile.write("  body { height: 100%; margin: 0px; padding: 0px }\n");
				textFile.write("  #map_canvas { height: 100% }\n");
				textFile.write("</style>\n");
				textFile.write("<title>" + replaced + " Elevation Profile</title> \n");
				textFile.write("<script type=\"text/javascript\">var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));</script>");
				textFile.write("    \n");
				textFile.write("<script type=\"text/javascript\">  var _gaq = _gaq || [];  _gaq.push(['_setAccount', 'UA-13084846-1']);  _gaq.push(['_trackPageview']);  (function() {    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  })();</script>");
				textFile.write("    \n");
				textFile.write("<script type=\"text/javascript\" src=\"http://www.google.com/jsapi\"></script>\n");
				textFile.write("<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>\n");
				textFile.write("<script type=\"text/javascript\"> \n");
				textFile.write("  var map = null;\n");
				textFile.write("  var chart = null;\n");
				textFile.write("  \n");
				textFile.write("  var geocoderService = null;\n");
				textFile.write("  var elevationService = null;\n");
				textFile.write("  var directionsService = null;\n");
				textFile.write("  \n");
				textFile.write("  var mousemarker = null;\n");
				textFile.write("  var markers = [];\n");
				textFile.write("  var polyline = null;\n");
				textFile.write("  var elevations = null;\n");
				textFile.write("  var icon = \"http://www.cyclingthealps.com/images/CTA_favicon.png\"\n");
				textFile.write("  \n");
				textFile.write("  var SAMPLES = 512;\n");
				textFile.write(" \n");
				textFile.write("  var examples = [{\n");
				textFile.write("    // Lombard St\n");
				textFile.write("    latlngs: [\n");
				Cell latlngCellX = sheet.getCell(1, i);
				String latlngX = latlngCellX.getContents();
				Cell latlngCellY = sheet.getCell(2, i);
				String latlngY = latlngCellY.getContents();
				Cell latlngCellXBegin = sheet.getCell(41, i);
				String latlngXBegin = latlngCellXBegin.getContents();
				Cell latlngCellYBegin = sheet.getCell(42, i);
				String latlngYBegin = latlngCellYBegin.getContents();
				Cell latlngCellXEnd = sheet.getCell(43, i);
				String latlngXEnd = latlngCellXEnd.getContents();
				Cell latlngCellYEnd = sheet.getCell(44, i);
				String latlngYEnd = latlngCellYEnd.getContents();

				textFile.write("      [" + latlngXBegin + "," + latlngYBegin + "],\n");
				textFile.write("      [" + latlngX + "," + latlngY + "],\n");
				textFile.write("      [" + latlngXEnd + "," + latlngYEnd + "]\n");
				textFile.write("    ],\n");
				textFile.write("    mapType: google.maps.MapTypeId.ROADMAP,\n");
				textFile.write("    travelMode: 'driving'\n");
				textFile.write("  }];\n");
				textFile.write(" \n");
				textFile.write("  // Load the Visualization API and the piechart package.\n");
				textFile.write("  google.load(\"visualization\", \"1\", {packages: [\"columnchart\"]});\n");
				textFile.write("  \n");
				textFile.write("  // Set a callback to run when the Google Visualization API is loaded.\n");
				textFile.write("  google.setOnLoadCallback(initialize);\n");
				textFile.write("  \n");
				textFile.write("  function initialize() {\n");
				textFile.write("    var latlng = new google.maps.LatLng(15, 0);\n");
				textFile.write("    var myOptions = {\n");
				textFile.write("      zoom: 1,\n");
				textFile.write("      center: latlng,\n");
				textFile.write("      mapTypeId: google.maps.MapTypeId.TERRAIN\n");
				textFile.write("    }\n");
				textFile.write(" \n");
				textFile.write("    map = new google.maps.Map(document.getElementById(\"map_canvas\"), myOptions);\n");
				textFile.write("	chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));\n");
				textFile.write("    \n");
				textFile.write("    geocoderService = new google.maps.Geocoder();\n");
				textFile.write("    elevationService = new google.maps.ElevationService();\n");
				textFile.write("    directionsService = new google.maps.DirectionsService();\n");
				textFile.write("    \n");
				textFile.write("   // google.maps.event.addListener(map, 'click', function(event) {\n");
				textFile.write("     // addMarker(event.latLng, true);\n");
				textFile.write("   // });\n");
				textFile.write("    \n");
				textFile.write("    google.visualization.events.addListener(chart, 'onmouseover', function(e) {\n");
				textFile.write("      if (mousemarker == null) {\n");
				textFile.write("        mousemarker = new google.maps.Marker({\n");
				textFile.write("          position: elevations[e.row].location,\n");
				textFile.write("          map: map,\n");
				textFile.write("          icon: icon\n");
				textFile.write("        });\n");
				textFile.write("      } else {\n");
				textFile.write("        mousemarker.setPosition(elevations[e.row].location);\n");
				textFile.write("      }\n");
				textFile.write("    });\n");
				textFile.write(" \n");
				textFile.write("    loadExample(0);\n");
				textFile.write("	updateElevation();\n");
				textFile.write("  }\n");
				textFile.write("  \n");
				textFile.write("  // Takes an array of ElevationResult objects, draws the path on the map\n");
				textFile.write("  // and plots the elevation profile on a GViz ColumnChart\n");
				textFile.write("  function plotElevation(results) {\n");
				textFile.write("    elevations = results;\n");
				textFile.write("    \n");
				textFile.write("    var path = [];\n");
				textFile.write("    for (var i = 0; i < results.length; i++) {\n");
				textFile.write("      path.push(elevations[i].location);\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    if (polyline) {\n");
				textFile.write("      polyline.setMap(null);\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    polyline = new google.maps.Polyline({\n");
				textFile.write("      path: path,\n");
				textFile.write("      strokeColor: \"#000000\",\n");
				textFile.write("      map: map});\n");
				textFile.write("    \n");
				textFile.write("    var data = new google.visualization.DataTable();\n");
				textFile.write("    data.addColumn('string', 'Sample');\n");
				textFile.write("    data.addColumn('number', 'Elevation');\n");
				textFile.write("    for (var i = 0; i < results.length; i++) {\n");
				textFile.write("      data.addRow(['', elevations[i].elevation]);\n");
				textFile.write("    }\n");
				textFile.write(" \n");
				textFile.write("    document.getElementById('chart_div').style.display = 'block';\n");
				textFile.write("    chart.draw(data, {\n");
				textFile.write("      //width: 512,\n");
				textFile.write("      //height: 200,\n");
				textFile.write("      legend: 'none',\n");
				textFile.write("      titleY: 'Elevation (m)',\n");
				textFile.write("      focusBorderColor: '#00ff00'\n");
				textFile.write("    });\n");
				textFile.write("  }\n");
				textFile.write("  \n");
				textFile.write("  // Remove the green rollover marker when the mouse leaves the chart\n");
				textFile.write("  function clearMouseMarker() {\n");
				textFile.write("    if (mousemarker != null) {\n");
				textFile.write("      mousemarker.setMap(null);\n");
				textFile.write("      mousemarker = null;\n");
				textFile.write("    }\n");
				textFile.write("  }\n");
				textFile.write("  \n");
				textFile.write("\n");
				textFile.write("  \n");
				textFile.write("  // Add a marker and trigger recalculation of the path and elevation\n");
				textFile.write("  function addMarker(latlng, doQuery) {\n");
				textFile.write("    if (markers.length < 10) {\n");
				textFile.write("      \n");
				textFile.write("      var marker = new google.maps.Marker({\n");
				textFile.write("        position: latlng,\n");
				textFile.write("        map: map,\n");
				textFile.write("        draggable: true\n");
				textFile.write("      })\n");
				textFile.write("      \n");
				textFile.write("      google.maps.event.addListener(marker, 'dragend', function(e) {\n");
				textFile.write("        updateElevation();\n");
				textFile.write("      });\n");
				textFile.write("      \n");
				textFile.write("      markers.push(marker);\n");
				textFile.write("      \n");
				textFile.write("      if (doQuery) {\n");
				textFile.write("        updateElevation();\n");
				textFile.write("      }\n");
				textFile.write("      \n");
				textFile.write("      if (markers.length == 10) {\n");
				textFile.write("        document.getElementById('address').disabled = true;\n");
				textFile.write("      }\n");
				textFile.write("    } else {\n");
				textFile.write("      alert(\"No more than 10 points can be added\");\n");
				textFile.write("    }\n");
				textFile.write("  }\n");
				textFile.write("  \n");
				textFile.write("  // Trigger the elevation query for point to point\n");
				textFile.write("  // or submit a directions request for the path between points\n");
				textFile.write("  function updateElevation() {\n");
				textFile.write("    if (markers.length > 1) {\n");
				textFile.write("      var travelMode = document.getElementById(\"mode\").value;\n");
				textFile.write("      if (travelMode != 'direct') {\n");
				textFile.write("        calcRoute(travelMode);\n");
				textFile.write("      } else {\n");
				textFile.write("        var latlngs = [];\n");
				textFile.write("        for (var i in markers) {\n");
				textFile.write("          latlngs.push(markers[i].getPosition())\n");
				textFile.write("        }\n");
				textFile.write("        elevationService.getElevationAlongPath({\n");
				textFile.write("          path: latlngs,\n");
				textFile.write("          samples: SAMPLES\n");
				textFile.write("        }, plotElevation);\n");
				textFile.write("      }\n");
				textFile.write("    }\n");
				textFile.write("  }\n");
				textFile.write("  \n");
				textFile.write("  // Submit a directions request for the path between points and an\n");
				textFile.write("  // elevation request for the path once returned\n");
				textFile.write("  function calcRoute(travelMode) {\n");
				textFile.write("    var origin = markers[0].getPosition();\n");
				textFile.write("    var destination = markers[markers.length - 1].getPosition();\n");
				textFile.write("    \n");
				textFile.write("    var waypoints = [];\n");
				textFile.write("    for (var i = 1; i < markers.length - 1; i++) {\n");
				textFile.write("      waypoints.push({\n");
				textFile.write("        location: markers[i].getPosition(),\n");
				textFile.write("        stopover: true\n");
				textFile.write("      });\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    var request = {\n");
				textFile.write("      origin: origin,\n");
				textFile.write("      destination: destination,\n");
				textFile.write("      waypoints: waypoints\n");
				textFile.write("    };\n");
				textFile.write("   \n");
				textFile.write("    switch (travelMode) {\n");
				textFile.write("      case \"bicycling\":\n");
				textFile.write("        request.travelMode = google.maps.DirectionsTravelMode.BICYCLING;\n");
				textFile.write("        break;\n");
				textFile.write("      case \"driving\":\n");
				textFile.write("        request.travelMode = google.maps.DirectionsTravelMode.DRIVING;\n");
				textFile.write("        break;\n");
				textFile.write("      case \"walking\":\n");
				textFile.write("        request.travelMode = google.maps.DirectionsTravelMode.WALKING;\n");
				textFile.write("        break;\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    directionsService.route(request, function(response, status) {\n");
				textFile.write("      if (status == google.maps.DirectionsStatus.OK) {\n");
				textFile.write("        elevationService.getElevationAlongPath({\n");
				textFile.write("          path: response.routes[0].overview_path,\n");
				textFile.write("          samples: SAMPLES\n");
				textFile.write("        }, plotElevation);\n");
				textFile.write("      } else if (status == google.maps.DirectionsStatus.ZERO_RESULTS) {\n");
				textFile.write("        alert(\"Could not find a route between these points\");\n");
				textFile.write("      } else {\n");
				textFile.write("        alert(\"Directions request failed\");\n");
				textFile.write("      }\n");
				textFile.write("    });\n");
				textFile.write("  }\n");
				textFile.write("   \n");
				textFile.write("  function loadExample(n) {\n");
				textFile.write("    reset();\n");
				textFile.write("    map.setMapTypeId(examples[n].mapType);\n");
				textFile.write("    document.getElementById('mode').value = examples[n].travelMode;\n");
				textFile.write("    var bounds = new google.maps.LatLngBounds();\n");
				textFile.write("    for (var i = 0; i < examples[n].latlngs.length; i++) {\n");
				textFile.write("      var latlng = new google.maps.LatLng(\n");
				textFile.write("        examples[n].latlngs[i][0],\n");
				textFile.write("        examples[n].latlngs[i][1]\n");
				textFile.write("      );\n");
				textFile.write("      addMarker(latlng, false);\n");
				textFile.write("      bounds.extend(latlng);\n");
				textFile.write("    }\n");
				textFile.write("    map.fitBounds(bounds);\n");
				textFile.write("    updateElevation();\n");
				textFile.write("  }\n");
				textFile.write("  \n");
				textFile.write("  // Clear all overlays, reset the array of points, and hide the chart\n");
				textFile.write("  function reset() {\n");
				textFile.write("    if (polyline) {\n");
				textFile.write("      polyline.setMap(null);\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    for (var i in markers) {\n");
				textFile.write("      markers[i].setMap(null);\n");
				textFile.write("    }\n");
				textFile.write("    \n");
				textFile.write("    markers = [];\n");
				textFile.write("    \n");
				textFile.write("    document.getElementById('chart_div').style.display = 'none';\n");
				textFile.write("  }\n");
				textFile.write(" \n");
				textFile.write("</script>\n");
				textFile.write("</head> \n");
				textFile.write("<body onload=\"initialize()\">\n");
				textFile.write("  <div id=\"map_canvas\" style=\"width:100%; height:70%\"></div>\n");
				textFile.write("\n");
				textFile.write("      <center id=\"mode\" onchange=\"updateElevation()\">\n");
				textFile.write("        \n");
				textFile.write("      </center>\n");
				textFile.write("  <div id=\"chart_div\" style=\"width:100%; height:27%\" onmouseout=\"clearMouseMarker() \"></div>\n");
				textFile.write("</body> \n");
				textFile.write("</html>\n");
				textFile.close();
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
